package com.xb.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xb.common.helper.MD5;
import com.xb.common.result.Result;
import com.xb.model.system.SysUser;
import com.xb.model.vo.SysUserQueryVo;
import com.xb.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xb
 * @since 2023-08-23
 */
//@Api(tags = "用户管理") //Swagger生成api的提示
//@RestController
//@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;


    /**
     *根据id删除
     *
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable("id") Long id) {
        boolean flag = sysUserService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @param userQueryVo
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}")
    public Result page(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "userQueryVo", value = "查询对象", required = false)
                    SysUserQueryVo userQueryVo) {
        Page<SysUser> pageParam = new Page<>(page, limit);
        IPage<SysUser> pageModel = sysUserService.selectPage(pageParam, userQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 添加
     * RequestBody 不用于get请求中
     * 将请求体的内容的JSON数据封装到实体类中,可以省略
     *
     * @param sysUser
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @ApiOperation(value = "添加")
    @PostMapping("/save")
    public Result save(@RequestBody SysUser sysUser) {
        String password = sysUser.getPassword();
        String encrypt = MD5.encrypt(password);
        sysUser.setPassword(encrypt);
        boolean flag = sysUserService.save(sysUser);
        return flag ? Result.ok() : Result.fail();
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        SysUser user = sysUserService.getById(id);
        return Result.ok(user);
    }

    /**
     * 根据id修改
     *
     * @param user
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "根据id修改")
    @PutMapping("/update")
    public Result update(@RequestBody SysUser user) {
        String password = user.getPassword();
        String encrypt = MD5.encrypt(password);
        user.setPassword(encrypt);
        boolean flag = sysUserService.updateById(user);
        return flag ? Result.ok() : Result.fail() ;
    }

    /**
     * 根据id批量删除
     * json的数组格式对应list集合
     * @param idList
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysUser.delete')")
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/batch")
    public Result batch(@RequestBody List<Long> idList) {
        sysUserService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 用户状态更新
     * @param id
     * @param status
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "更新状态")
    @GetMapping("status/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysUserService.updateStatus(id, status);
        return Result.ok();
    }

    /**
     * 获取个人信息
     * @return
     */
    @ApiOperation(value = "获取个人信息")
    @GetMapping("personal")
    public Result get(Principal principal) {
        String name = principal.getName();
        SysUser sysUser = sysUserService.getByUsername(name);
        return Result.ok(sysUser);
    }
    /**
     * 修改个人信息
     * @return
     */
    @ApiOperation(value = "修改个人信息")
    @PostMapping("mod")
    public Result mod(@RequestBody SysUser sysUser) {
        sysUserService.save(sysUser);
        return Result.ok();
    }

}

